/**
 * 
 */
package com.huarui.user.security;

import java.util.Date;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.ewaf.framework.cookie.CookieManager;
import org.ewaf.framework.struts2.util.DateFormatUtil;
import org.ewaf.framework.util.ParamUtil;

import com.huarui.user.model.CookieConstants;
import com.huarui.user.model.SessionMessage;

/**
 * @author peng.sunp
 * 
 */
public class UserCookieAuther {

	/**
	 * 根据cookie信息获取当前用户
	 * 
	 * @param cookie
	 * @return
	 */
	public static SessionMessage getSessionMessage(CookieManager cookie) {
		SessionMessage sessionMessage = new SessionMessage();

		String userId = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_ID);

		if (StringUtils.isNotBlank(userId)) {

			sessionMessage.setUserId(Integer.parseInt(userId));

		} else {

			return null;

		}

		String companyId = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.COMPANYT_ID);

		if (StringUtils.isNotBlank(companyId)) {

			sessionMessage.setCompanyId(Integer.valueOf(companyId));
		}
		
		String companyType = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.COMPANYTYPE);

		if (StringUtils.isNotBlank(companyType)) {
			sessionMessage.setCompanyType(Integer.valueOf(companyType));
		}

		String employeeId = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.EMPLOYEEID);

		if (StringUtils.isNotBlank(employeeId)) {
			sessionMessage.setEmployeeId(Integer.valueOf(employeeId));
		}

		String lastLoginDate = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_LAST_LOGIN);

		if (StringUtils.isNotBlank(lastLoginDate)){
			Date lastLogin = DateFormatUtil.parseDate(lastLoginDate);
			sessionMessage.setUserLastLoginTime(lastLogin);
		}
		
		
		String roles = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.ROLES);

		if (StringUtils.isNotBlank(roles) && !"null".equals(roles)) {

			sessionMessage.setRoles(Integer.parseInt(roles));
		}

		String logintimes = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_LOGINTIMES);

		sessionMessage.setUserLoginTimes(Integer.valueOf(logintimes));

		String email = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_EMAIL);

		String name = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_NAME);

		String ident = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_IDENT);

		String companyStatus = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.STATUS);

		String pyCode = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_PY);

		String logined = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.LOGINED);
		if (StringUtils.isNotBlank(logined) && !"null".equals(logined)) {

			sessionMessage.setLogined(Boolean.parseBoolean(logined));
		}

		String authenticationType = cookie.getValue(CookieConstants.COOKIE_PERM,CookieConstants.AUTHENTICATION_TYPE);

		sessionMessage.setAuthenticationCompany(Integer.valueOf(authenticationType));

		String list_roles = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.LIST_ROLES);

		sessionMessage.setRoleIds(ParamUtil.str2List(list_roles));

		String companyName = cookie.getValue(CookieConstants.COOKIE_PERM,
				CookieConstants.COMPANY_NAME);
		sessionMessage.setCompanyName(companyName);
		sessionMessage.setUserEmail(email);
		sessionMessage.setUserName(name);
		sessionMessage.setUserPy(pyCode);
		sessionMessage.setCompanyStatus(companyStatus);
		sessionMessage.setUserIdent(ident);
		return sessionMessage;

	}

	/**
	 * 把当前会话信息写入cookie
	 * 
	 * @param user
	 * @param cookie
	 * @param response
	 */
	public static void writeSessionMessage(SessionMessage sessionMessage,
			CookieManager cookie, HttpServletResponse response) {

		Integer userId = sessionMessage.getUserId();

		if (userId == null || userId == 0) {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.USER_ID, "");

		} else {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.USER_ID, String.valueOf(userId));

		}
		Integer companyId = sessionMessage.getCompanyId();

		if (companyId == null) {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.COMPANYT_ID, "");
		} else {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.COMPANYT_ID, String.valueOf(companyId));

		}

		Integer companyType = sessionMessage.getCompanyType();

		if (companyType == null) {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.COMPANYTYPE, "");
		} else {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.COMPANYTYPE, String.valueOf(companyType));
		}

		Integer employeeId = sessionMessage.getEmployeeId();

		if (employeeId == null) {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.EMPLOYEEID, "");
		} else {

			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.EMPLOYEEID, String.valueOf(sessionMessage
							.getEmployeeId()));
		}

		Date userLastLoginTime = sessionMessage.getUserLastLoginTime();
		
		if(userLastLoginTime==null){
			
			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.USER_LAST_LOGIN,"");
		}else{
			
			cookie.setValue(CookieConstants.COOKIE_PERM,
					CookieConstants.USER_LAST_LOGIN, DateFormatUtil
							.date(sessionMessage.getUserLastLoginTime()));		
		}	
		
		cookie.setValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_LOGINTIMES, String.valueOf(sessionMessage
						.getUserLoginTimes()));

		cookie.setValue(CookieConstants.COOKIE_PERM,
				CookieConstants.AUTHENTICATION_TYPE, String
						.valueOf(sessionMessage.getAuthenticationCompany()));

		cookie.setValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_EMAIL, sessionMessage.getUserEmail());
		cookie.setValue(CookieConstants.COOKIE_PERM, CookieConstants.USER_NAME,
				sessionMessage.getUserName());
		cookie.setValue(CookieConstants.COOKIE_PERM,
				CookieConstants.USER_IDENT, sessionMessage.getUserIdent());
		cookie.setValue(CookieConstants.COOKIE_PERM, CookieConstants.LOGINED,
				String.valueOf(true));
		cookie.setValue(CookieConstants.COOKIE_PERM, CookieConstants.USER_PY,
				sessionMessage.getUserPy());
		cookie.setValue(CookieConstants.COOKIE_PERM,
				CookieConstants.COMPANY_NAME, sessionMessage.getCompanyName());
		cookie.setValue(CookieConstants.COOKIE_PERM, CookieConstants.ROLES,
				String.valueOf(sessionMessage.getRoles()));

		cookie.setValue(CookieConstants.COOKIE_PERM, CookieConstants.ROLEIDS,
				String.valueOf(sessionMessage.getRoles()));

		cookie.setValue(CookieConstants.COOKIE_PERM, CookieConstants.STATUS,
				String.valueOf(sessionMessage.getCompanyStatus()));

		cookie.setValue(CookieConstants.COOKIE_PERM,
				CookieConstants.LIST_ROLES, ParamUtil.list2Str(sessionMessage
						.getRoleIds()));

		cookie.saveCookie(response, null, CookieConstants.Cookie_PATH,
				CookieConstants.COOKIE_PERM, CookieConstants.PERM_COOKIE_AGE);

	}

}
